package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.ReservationDto;
import com.zzyl.dto.ReservationQueryDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.properties.JwtTokenProperties;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ThreadLocalUtil;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.net.http.HttpRequest;
import java.time.*;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtTokenProperties jwtTokenProperties;

    /**
     * 分页查找预约
     */
    @Override
    public PageBean<ReservationVo> findByPage(ReservationQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Long userId = ThreadLocalUtil.get();
        dto.setUserId(userId);
        Page<Reservation> page = reservationMapper.findByPage(dto);
        return PageBean.of(page, ReservationVo.class);
    }

    /**
     * 查询取消预约数量
     *
     * @return {@code Integer }
     * @author YiSheep
     */
    @Override
    public Integer getCancelledCount() {
        //LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime startTime = LocalDateTime.now().with(LocalTime.MIN);
        //LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));
        LocalDateTime endTime = LocalDateTime.now().with(LocalTime.MAX);
        Long id = ThreadLocalUtil.get();
        int cancelledCount = reservationMapper.getCancelledCount(startTime, endTime, id);
        return cancelledCount;
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time 预约日期
     * @return {@code TimeCountVo }
     * @author YiSheep
     */
    @Override
    public List<TimeCountVo> countByTimeRange(Long time) {
        //将时间戳转换为LocalDateTime
        LocalDateTime startTime = LocalDateTimeUtil.of(time).with(LocalTime.MIN);
        LocalDateTime endTime = LocalDateTimeUtil.of(time).with(LocalTime.MAX);
        List<TimeCountVo> timeCountVos = reservationMapper.countByTimeRange(startTime, endTime);
        return timeCountVos;
    }

    /**
     * 新增预约
     *
     * @param dto 预约Dto
     * @author YiSheep
     */
    @Override
    public void addReservation(ReservationDto dto) {
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        reservation.setStatus(0);
        reservationMapper.insert(reservation);
    }

    /**
     * 取消预约
     *
     * @param id 预约ID
     * @author YiSheep
     */
    @Override
    public void cancelReservation(Long id) {
        Reservation reservation = Reservation.builder().id(id).status(2).build();
        reservationMapper.update(reservation);
    }
}

